const menus = ["/user", "/role", "/major", "/student"]

const permissions = [
  {
    title: "首页",
    icon: "HomeFilled",
    index: "/home",
  },
  {
    title: "用户管理",
    icon: "UserFilled",
    index: "/user",
  },
  {
    title: "角色管理",
    icon: "WalletFilled",
    index: "/role",
  },
  {
    title: "学员管理",
    icon: "Avatar",
    index: "4",
    children: [
      {
        title: "学校管理",
        icon: "School",
        index: "/school",
      },
      {
        title: "专业管理",
        icon: "HelpFilled",
        index: "/major",
      },
      {
        title: "班级管理",
        icon: "Management",
        index: "/class",
      },
      {
        title: "学生管理",
        icon: "Stamp",
        index: "/student",
      },
    ],
  },
]

// 根据 menus 得到符合 permissions结构的结果
// 用来装结果的
// const resArr = []

// for (let i = 0; i < permissions.length; i++) {
//   if (menus.includes(permissions[i].index)) {
//     resArr.push(permissions[i])
//   }
//   if (permissions[i].children) {
//     // 需要再次对choldren进行过滤
//     for (let j = 0; j < permissions[i].children.length; j++) {
//       if (menus.includes(permissions[i].children[j].index)) {
//         // 装进去
//         // 装到哪里
//         resArr.push({
//           title: "学员管理",
//           icon: "xxx",
//           index: "xxx",
//           children: [permissions[i].children[j]],
//         })
//       }
//     }
//   }
// }

function filter(permissions, menus) {
  const resArr = []
  for (let i = 0; i < permissions.length; i++) {
    if (menus.includes(permissions[i].index)) {
      resArr.push(permissions[i])
    }
    // 如果有子项
    if (permissions[i].children) {
      const r = filter(permissions[i].children, menus)
    //   console.log(r)
      resArr.push({
        ...permissions[i],
        children: r
      })
    }
  }
  return resArr
}

const resArr = filter(permissions, menus)

console.log(resArr)
